﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace yao.Common
{
    public static class FunExtentions
    {
        public static string Moneyformat
        {
            get
            {
                return "{0:0.00}";
            }
        }
        public static string MoneyFormat(this decimal? money) {
            if (money == null) {
                return string.Empty;
            }
            return ((decimal)money).MoneyFormat();
        }
        public static string MoneyFormat(this decimal money)
        {
            return ((decimal)money).ToString("N");
        }
        public static string Timeformat
        {
            get
            {
                return "{0:yyyy-MM-dd}";
            }
        }
        public static string TimeFormat(this DateTime? datetime)
        {
            if (datetime == null)
            {
                return string.Empty;
            }
            return ((DateTime)datetime).TimeFormat();
        }
        public static string TimeFormat(this DateTime? datetime, string format)
        {
            if (datetime == null)
            {
                return string.Empty;
            }
            return ((DateTime) datetime).TimeFormat(format);
        }
        public static string TimeFormat(this DateTime datetime)
        {
            return datetime.TimeFormat("yyyy-MM-dd HH:mm:ss");
        }

        public static string TimeFormat(this DateTime datetime,string format)
        {
            return datetime.ToString(format);
        }

        public static string Quantityformat
        {
            get
            {
                return "{0:0.00}";
            }
        }
        /// <summary>
        /// 数量格式化
        /// </summary>
        /// <param name="money"></param>
        /// <returns></returns>
        public static string QuantityFormat(this decimal money)
        {
            return ((decimal)money).ToString("N");
        }

        public static string GetEnumDescription(this Enum value)
        {
            try
            {
                FieldInfo fi = value.GetType().GetField(value.ToString());
                if (fi == null) return string.Empty;
                DescriptionAttribute[] attributes =
                    (DescriptionAttribute[])fi.GetCustomAttributes(
                    typeof(DescriptionAttribute),
                    false);

                if (attributes != null &&
                    attributes.Length > 0)
                    return attributes[0].Description;
                else
                    return value.ToString();
            }
            catch (Exception ex)
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 状态转化
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        public static string StateFormat(this bool state)
        {
            return state ? "启用" : "禁用";
        }

        
    }
}
